package service

import (
	"seat-service/initialization"
	"seat-service/model"
	"seat-service/model/dto"
	"time"
)

type ClickReserveService struct {
}

// GetOptionalSeats 查找该教室所有可用的位置
func (c *ClickReserveService) GetOptionalSeats(campusID, buildingID, classroomID int) (ids []*dto.ClickReserve, err error) {
	err = initialization.DB.Model(&model.Seat{}).Where("campus_id = ? AND building_id = ? AND classroom_id = ? AND seat_status = ?", campusID, buildingID, classroomID, 0).Find(&ids).Error
	return
}

// GetOptionalSeatsOne 判断预约中的位置是否可选
func (c *ClickReserveService) GetOptionalSeatsOne(id int, startTime, endTime time.Time) (bool, error) {
	var count int64
	err := initialization.DB.Model(&model.Reserve{}).
		Where("seat_id = ? AND NOT (appointment_time_start >= ? OR appointment_time_end <= ?)", id, endTime, startTime).
		Count(&count).Error
	if err != nil {
		return false, err
	}
	// 判断如果count>0，就是说明有与预订时间有冲突的
	if count > 0 {
		return false, nil
	}
	// 如果count=0，就是说明找不到与预订时间有冲突的
	return true, nil
}

// GetOptionalSeatsTwo 判断使用中的位置是否可选
func (c *ClickReserveService) GetOptionalSeatsTwo(id int, startTime time.Time) (bool, error) {
	var count int64
	err := initialization.DB.Model(&model.Reserve{}).Where("seat_id = ? AND NOT appointment_time_end <= ?", id, startTime).
		Count(&count).Error
	if err != nil {
		return false, err
	}
	if count > 0 {
		return false, nil
	}
	return true, nil
}
